package com.moss.street.market;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * A controller servlet used by the Welcome.jsp view for information on market dates.
 * 
 * @author drusk
 * 
 */

public class MarketDaysServlet extends HttpServlet implements Servlet {
	
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		Calendar c = Calendar.getInstance();
		// Set the calendar to SATURDAY of the current week
		c.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);

		// Print upcoming Saturdays starting with current week
		// for the next year
		DateFormat df = new SimpleDateFormat("EEE dd-MMM-yyyy");
		DateFormat dfdb = new SimpleDateFormat("yyyy-MM-dd");
		
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.println("<select name=\"marketday\" size=\"10\">");
		for (int i = 0; i <= 52; i++) {
			out.print(" <option value=\"" + dfdb.format(c.getTime()) + "\"");
			out.println(">" + df.format(c.getTime()) + "</option>");
			c.add(Calendar.DATE, 7);
		}
		out.println("</select>\n");
	}
	
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

}
